package com.uottawa.clickerapp.views;

import com.uottawa.clickerapp.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class QuestionAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String title;
    private final String[] questions;

    public QuestionAdapter(Activity context, String[] questions, String title) {
        super(context, R.layout.rowlayout);
        this.context = context;
        this.title = title;
        this.questions = questions;
    }

    // static to save the reference to the outer class and to avoid access to
    // any members of the containing class
    class ViewHolder {
        //public ImageView logo;
        public TextView  title;
        public TextView  rowdata;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // ViewHolder will buffer the assess to the individual fields of the row
        // layout

        ViewHolder holder;
        // Recycle existing view if passed as parameter
        // This will save memory and time on Android
        // This only works if the base layout for all classes are the same
        View rowView = convertView;
        if (rowView == null) 
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.rowlayout, null, true);
            holder = new ViewHolder();
            holder.title = (TextView) rowView.findViewById(R.id.thetitle);
            holder.rowdata = (TextView) rowView.findViewById(R.id.listnames);
            rowView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder) rowView.getTag();
        }

        holder.title.setText(title);
        holder.rowdata.setText(questions[position]);

        return rowView;
    }
}  
